using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.GameSound;

namespace MainGame.GamePlay
{
  /// <summary>
  /// Gestiamo la logica del giocatore, ascoltando l'evento di fine turno e
  /// i clicks su una carta
  /// </summary>
  public abstract class PlayerLayer : AILayer
  {

    public PlayerLayer(Game game)
      : base(game)
    {
      UpdateOrder = (int)ComponentOrders.LOGIC;
    }

    public override void Initialize()
    {
      Game.GetBoardRenderer().ClickPlayerCard += new ClickPlayerCard(BoardManager_ClickPlayerCard);
      Game.GetBoardRenderer().ReleasePlayerCard += new ReleasePlayerCard(BoardManager_ReleasePlayerCard);
      Game.GetBoardRenderer().ClickPack += new ClickPack(BoardManager_ClickPack); 
      Game.GetUIRenderer().OnTurnOver += new OnTurnOver(BoardManager_OnTurnOver);

      base.Initialize();
    }

    protected virtual void BoardManager_ClickPlayerCard(int player_index, int card_index)
    {
      var b = players[player_index].Cards[card_index].Selected;
      players[player_index].Cards.ForEach(c => c.Selected = false);
      players[player_index].Cards[card_index].Selected = !b;
      var sound_manager = Game.Services.GetService(typeof(ISoundManager<GamePlaySounds>)) as ISoundManager<GamePlaySounds>;
      sound_manager.PlaySound(GamePlaySounds.GamePlaySoundA);
    }

    protected virtual void BoardManager_ReleasePlayerCard(int player_index, int card_index)
    {
        //var b = players[player_index].Cards[card_index].Selected;
        players[player_index].Cards.ForEach(c => c.Selected = false);
        //players[player_index].Cards[card_index].Selected = !b;
        var sound_manager = Game.Services.GetService(typeof(ISoundManager<GamePlaySounds>)) as ISoundManager<GamePlaySounds>;
        //sound_manager.PlaySound(GamePlaySounds.GamePlaySoundA);
    }

    /// <summary>
    /// Estrae nuove carte dal mazzo e le assegna al giocatore
    /// </summary>
    /// <param name="player_index">L'indice del giocatore</param>
    /// <param name="card_quantity">Il numero di carte da estrarre</param>
    void BoardManager_ClickPack(int player_index, int card_quantity)
    {
      if (pack.Cards.Count() > 0)
      {
        for (int i = 0; i < card_quantity; ++i)
        {
          players[player_index].Cards.Add(pack.Cards.First());
          pack.Cards = pack.Cards.Skip(1).ToList();
        }
      }
    }

    void BoardManager_OnTurnOver()
    {
      if (CurrentPlayer.IsHuman)
      {
        if (CurrentPlayer.Cards.Any(c => c.Selected))
        {
          var card = CurrentPlayer.Cards.First(c => c.Selected);
          if (blocks.Any() && blocks.First().Cards.Any())
          {
            if (card.Color != blocks.First().Cards.First().Color)
              return;
          }
          CurrentPlayer.Cards.Remove(card);
          blocks.Add(new CardBlock() { Cards = new List<Card>() { card }, PlayedBy = CurrentPlayer });
          TurnOver();
        }
      }
    }
  }
}
